import 'dart:collection';

import 'package:WeChat/config.dart';
/**
 * @Author: zhouge
 * @Description:  谈话界面
 * @Date: Created in 7:34 2020-05-18
 * @Modified By:
 **/
import 'package:flutter/material.dart';
import 'package:WeChat/model/conver.dart';

class _ConversationItem extends StatelessWidget {
  //设置参数传入 构造器
   _ConversationItem({Key key, this.conversation})
      : assert(conversation != null),
        super(key: key);

  final Conversation conversation;
  var tapPos;

  _showMenu(BuildContext context, Offset tapPos) {
    final RenderBox overlay = Overlay.of(context).context.findRenderObject();
    final RelativeRect position = RelativeRect.fromLTRB(tapPos.dx, tapPos.dy,
        overlay.size.width - tapPos.dx, overlay.size.height - tapPos.dy);
    showMenu<String>(
        context: context,
        position: position,
        items: <PopupMenuItem<String>>[
          PopupMenuItem(
            child: Text(Constants.MENU_MASK_AS_UNREAD),
            value: Constants.MENU_MASK_AS_UNREAD_VALUE,
          ),
          PopupMenuItem(
            child: Text(Constants.MENU_PIN_TO_TOP),
            value: Constants.MENU_PIN_TO_TOP_VALUE,
          ),
          PopupMenuItem(
            child: Text(Constants.MENU_DELETE_CONVERSATION),
            value: Constants.MENU_DELETE_CONVERSATION_VALUE,
          ),
        ]).then<String>((String selected) {
          switch(selected){
            default:
              print('点击的菜单是：$selected');
          }
    });
  }

  @override
  Widget build(BuildContext context) {
    // 根据图片的获取方式初始化头像组件
    Widget avatar;
    if (conversation.isAvatarFromNet()) {
      avatar = Image.network(conversation.avatar,
          width: Constants.ConversationAvatarSize,
          height: Constants.ConversationAvatarSize);
    } else {
      avatar = Image.asset(
        conversation.avatar,
        width: Constants.ConversationAvatarSize,
        height: Constants.ConversationAvatarSize,
      );
    }

    Widget avatarContainer;
    if (conversation.unreadMsgCount > 0) {
      // 头像未读消息角标
      Widget unreadMsgCountText = Container(
          width: Constants.UnreadMsgNotifyDotSize,
          height: Constants.UnreadMsgNotifyDotSize,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius:
                BorderRadius.circular(Constants.UnreadMsgNotifyDotSize / 2.0),
            color: Color(AppColors.NotifyDotBg),
          ),
          // 显示的文本内容
          child: Text(
            conversation.unreadMsgCount.toString(), //传参  未读消息
            style: AppStyles.UnreadMsgCountDotStyle,
          ));
      // 堆栈式布局 (元素叠加元素，元素布局,后面叠加前面的元素)
      avatarContainer = Stack(
        overflow: Overflow.visible, // 超出容器部分可以显示！
        children: <Widget>[
          avatar,
          Positioned(
            right: -6.0,
            top: -6.0,
            child: unreadMsgCountText,
          ),
        ],
      );
    } else {
      avatarContainer = avatar;
    }

    var _rightArea = <Widget>[
      Text(
        conversation.updateAt,
        style: AppStyles.DesStyle,
      ),
      Container(
        height: 5.0,
      )
    ];

    // 静音图标的调整以及修饰
    if (conversation.isMute) {
      _rightArea.add(Icon(Icons.notifications_off,
          color: Color(
            AppColors.ConversationMuteIcon,
          )));
    } else {
      _rightArea.add(
          Icon(Icons.notifications_off, color: Colors.transparent) // 设置成透明色
          );
    }
    // 返回主题架构
    return InkWell(
      onTap: () {print('打开会话：${conversation.title}');},
      onTapDown: (TapDownDetails details){
        tapPos = details.globalPosition;
      },
      onLongPress: () {_showMenu(context, tapPos);},
      child: Container(
        padding: const EdgeInsets.all(10.0),
        decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
                  color: Color(AppColors.DividerColor),
                  width: Constants.DividerWidth)),
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          // mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
//          Image.asset(
//            'assets/images/default_nor_avatar.png',
//             conversation.avatar,
//             width: Constants.ConversationAvatarSize,
//            height: Constants.ConversationAvatarSize,
//          ),

            avatarContainer, //使用新的方式进行替换
            Container(
              width: 10.0,
            ), //处理间隔常用Container容器
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(conversation.title, style: AppStyles.TitleStyle),
                  Text(
                    conversation.des,
                    style: AppStyles.DesStyle,
                  ),
                ],
              ),
            ),
            Column(
              children: _rightArea,
            ),
          ],
        ),
      ),
    );
  }
}

class ConversationPage extends StatefulWidget {
  @override
  _ConversationPageState createState() => _ConversationPageState();
}

// 调用函数
class _ConversationPageState extends State<ConversationPage> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        //itemExtent: 100.0,
        itemCount: ConversationPageData.mockConversations.length,
        itemBuilder: (BuildContext context, int index) {
          return _ConversationItem(
              conversation: ConversationPageData.mockConversations[index]);
        });
  }
}
